package gl.android.biyiqing.fragment;

import gl.API;
import gl.android.biyiqing.R;
import gl.android.biyiqing.adapter.HuBeiQuYuAdapter;
import gl.android.biyiqing.adapter.ShenFenAdapter;
import gl.android.biyiqing.bean.BYQUserInfo;
import gl.android.biyiqing.bean.KV;
import gl.android.biyiqing.competent.ZEYOUCallBack;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.AndroidUtil;
import gl.uitls.Lg;
import gl.uitls.Tip;
import gl.view.SimpleDataPickerDialog;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.edmodo.rangebar.RangeBar;

public class UserInfoFragment extends BaseFragment implements
		View.OnClickListener {
	// Content View Elements

	// Content View Elements

	private LinearLayout container;
	private LinearLayout container_neixingaobai;
	private LinearLayout container_zeyou;
	private boolean isEditAble = false;

	// End Of Content View Elements

	private void bindViews() {
		container = (LinearLayout) rootView.findViewById(R.id.container);
		container_neixingaobai = (LinearLayout) rootView
				.findViewById(R.id.container_neixingaobai);
		container_zeyou = (LinearLayout) rootView
				.findViewById(R.id.container_zeyou);
		findViewById(R.id.textView1).setOnClickListener(this);
		findViewById(R.id.textView2).setOnClickListener(this);
		findViewById(R.id.textView3).setOnClickListener(this);
		((TextView) findViewById(R.id.textView1)).setEnabled(false);
		((TextView) findViewById(R.id.textView3)).setEnabled(false);
		setAllChildViewEnable(container, (false));
		setAllChildViewEnable(container_neixingaobai, (false));
		setAllChildViewEnable(container_zeyou, (false));
	}

	void setAllChildViewEnable(View view, boolean enable) {
		if (view instanceof ViewGroup) {
			ViewGroup vb = (ViewGroup) view;
			for (int i = 0; i < vb.getChildCount(); i++) {
				setAllChildViewEnable(vb.getChildAt(i), enable);
			}
		}
		else {
			view.setEnabled(enable);
		}
	}

	public UserInfoFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_user_info, container,
				false);
		isEditAble = UserManger.getInstance().userid.equals(userID) ? true
				: false;
		bindViews();
		initData();
		return rootView;
	}

	private EditText tv_monlog;
	private String monolog;
	// String s =
	// "生日：birthday，身高：height，体重：weight，学历：education，婚姻状况：marrystatus，职业：jobs，收入：salary，籍贯省：nationprovinceid，籍贯市：nationcityid，所在地市：cityid，所在地区：distid，居住情况：housing，购车情况：caring，民族：national，星座：astro，个性：personality，兴趣：interest";
	public final static String keys[] = { "birthday", "height", "weight",
			"education", "marrystatus", "jobs", "salary", "provinceid",
			"areas", "housing", "caring", "national", "astro", "personality",
			"interest" };
	public final static String keys_ZEYOU[] = { "gender", "age", "height",
			"education", "marrystatus", "avatar" };
	// public final static String keys[] =
	// {"birthday","height","weight","education","marrystatus","jobs","salary","nationprovinceid","nationcityid","cityid","distid","housing","caring","national","astro","personality","interest"};
	/**
	 * 个人资料的KEY列表
	 */
	public final static ArrayList<String> KEYLIST4USERINFO = new ArrayList<String>();
	public final static ArrayList<String> KEYLIST4ZEYOUINFO = new ArrayList<String>();
	static {
		for (int i = 0; i < keys.length; i++) {
			KEYLIST4USERINFO.add(keys[i]);
		}
		for (int i = 0; i < keys_ZEYOU.length; i++) {
			KEYLIST4ZEYOUINFO.add(keys_ZEYOU[i]);
		}
	}

	public void genIntInputDialog(final String message, final String title,
			final TextView regist_email2) {
		final EditText et = new EditText(regist_email2.getContext());
		et.setInputType(InputType.TYPE_CLASS_NUMBER);
		// 设置最大输入字符为3
		et.setFilters(new InputFilter[] { new InputFilter.LengthFilter(3) });
		AndroidUtil.genCustomViewDialog(regist_email2.getContext(), et,
				message, title, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						try {
							if (et.getText() != null) {

								regist_email2.setTag(et.getText().toString());
								regist_email2.setText(et.getText().toString());
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				});
	}

	public static void genPhoneNumberInputDialog(final String message,
			final String title, final TextView regist_email2) {
		final EditText et = new EditText(regist_email2.getContext());
		et.setInputType(InputType.TYPE_CLASS_NUMBER);
		// 设置最大输入字符为3
		et.setFilters(new InputFilter[] { new InputFilter.LengthFilter(3) });
		AndroidUtil.genCustomViewDialog(regist_email2.getContext(), et,
				message, title, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						try {
							if (et.getText() != null) {

								regist_email2.setTag(et.getText().toString());
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				});
	}

	/**
	 * 生成两次确认的Dialog 第二次出现在第一个后面
	 * 
	 * @param message
	 * @param title
	 * @param regist_email2
	 */
	public static void genIntInputDialog2(final String message[],
			final String title, final TextView regist_email2,
			final ZEYOUCallBack callback) {
		// final EditText et = new EditText(regist_email2.getContext());
		// et.setInputType(InputType.TYPE_CLASS_NUMBER);
		// // 设置最大输入字符为3
		// et.setFilters(new InputFilter[] { new InputFilter.LengthFilter(3) });
		// AndroidUtil.genCustomViewDialog(regist_email2.getContext(), et,
		// !callback.ing ? message[0] : message[1], title,
		// new DialogInterface.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		//
		// try {
		// if (et.getText() != null
		// && et.getText().length() > 0) {
		// final String string = et.getText().toString();
		// if (callback.ing) {
		// final String tag = regist_email2.getTag()
		// .toString();
		// regist_email2.setText(tag + "-" + string);
		//
		// regist_email2.setTag(tag
		// + BYQUserInfo.ZEYOU_ConnectChar
		// + string);
		// callback.callback(regist_email2);
		// }
		// else {
		// regist_email2.setTag(null);
		// regist_email2.setTag(string);
		// callback.ing = true;
		// genIntInputDialog2(message, title,
		// regist_email2, callback);
		// }
		//
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// dialog.dismiss();
		// }
		// });

		LayoutInflater ly = (LayoutInflater) regist_email2.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View rootview = ly.inflate(R.layout.dialog_rangebar, null);
		final TextView tv_rangeProgress = (TextView) rootview
				.findViewById(R.id.tv_rangeProgress);
		boolean isChangRange = false;
		for (String string : message) {
			if (string.contains("年龄")) {
				isChangRange = true;
			}
		}
		final RangeBar rangeBar = (RangeBar) rootview
				.findViewById(R.id.rangebar);
		if (isChangRange) {
			rangeBar.setTickCount(61);
		}
		else {
			rangeBar.setTickCount(200);
		}
		rangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {

			@Override
			public void onIndexChangeListener(RangeBar rangeBar,
					int leftThumbIndex, int rightThumbIndex) {
				tv_rangeProgress.setText(leftThumbIndex
						+ BYQUserInfo.ZEYOU_ConnectChar + rightThumbIndex);
				tv_rangeProgress.setTag(leftThumbIndex
						+ BYQUserInfo.ZEYOU_ConnectChar + rightThumbIndex);
			}
		});

		AndroidUtil.genCustomViewDialog(regist_email2.getContext(), rootview,
				"", "请拖动以选择一个范围", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						try {
							if (tv_rangeProgress.getText() != null
									&& tv_rangeProgress.getText().length() > 0) {
								final String string = tv_rangeProgress
										.getText().toString();
								final String tag = tv_rangeProgress.getTag()
										.toString();
								regist_email2.setText(string);

								regist_email2.setTag(tag);
								callback.callback(regist_email2);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
						dialog.dismiss();
					}
				});
	}

	/**
	 * 生成两次确认的Dialog 第二次出现在第一个后面
	 * 
	 * @param readableArray
	 * @param regist_nick2
	 */
	protected void genRaidoListDialog2(final String[] readableArray,
			final TextView regist_nick2, final ZEYOUCallBack callBack) {
		if (readableArray == null) {
			Tip.show("无可编辑项");
			return;
		}
		final ListView listview = new ListView(getActivity());
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				R.layout.item_list_xingqu, android.R.id.text1);
		int i = 0;
		for (String s : readableArray) {
			if (i != 0) {
				adapter.add(s);
			}
			i++;
		}
		final AlertDialog dialog = AndroidUtil.genCustomViewDialog(
				getActivity(), listview).show();
		;
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (callBack.ing) {
					final Integer valueOf = Integer.valueOf(regist_nick2
							.getTag().toString());
					regist_nick2.setText(readableArray[valueOf.intValue() - 1]
							+ "-" + (readableArray[position + 1]));
					regist_nick2.setTag(regist_nick2.getTag() + ","
							+ (position + 1));
					callBack.callback(regist_nick2);
				}
				else {
					regist_nick2.setTag(null);
					regist_nick2.setTag("" + Integer.valueOf(position + 1));
					callBack.ing = true;
					genRaidoListDialog2(readableArray, regist_nick2, callBack);
				}
				dialog.dismiss();
			}
		});

	}

	protected void genDateDialog(final TextView tv) {
		final SimpleDataPickerDialog simpleDataPickerDialog = new SimpleDataPickerDialog(
				getActivity(), 0, new DatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker arg0, int year, int month,
							int day) {
						tv.setText("" + year + "-" + (month + 1) + "-" + day);
						tv.setTag("" + year + "-" + (month + 1) + "-" + day);
					}
				}, 0, 0, 0);
		simpleDataPickerDialog.show();
	}

	protected void genRaidoListDialog(final String[] readableArray,
			final TextView regist_nick2) {
		if (readableArray == null) {
			Tip.show("无可编辑项");
			return;
		}
		final ListView listview = new ListView(getActivity());
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				R.layout.item_list_xingqu, android.R.id.text1);
		int i = 0;
		for (String s : readableArray) {
			if (i != 0) {
				adapter.add(s);
			}
			i++;
		}
		final AlertDialog dialog = AndroidUtil.genCustomViewDialog(
				getActivity(), listview).show();
		;
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				regist_nick2.setTag(position + 1);
				regist_nick2.setText(readableArray[position + 1]);
				dialog.dismiss();
			}
		});

	}

	protected void showShenFenSelectDialog(final TextView tv) {
		final ExpandableListView listview = new ExpandableListView(
				tv.getContext());
		final AlertDialog dialog = AndroidUtil.genCustomViewDialog(
				tv.getContext(), listview).show();
		listview.setAdapter(new ShenFenAdapter(tv.getContext(),
				new ShenFenAdapter.OnSelect() {

					@Override
					public void onselect(String gid, String gname,
							String childid, String cname) {
						dialog.dismiss();
						tv.setTag(childid);
						tv.setText(cname);
					}
				}));

	}

	protected void showHuBeiQuYuSelectDialog(final TextView tv) {
		final ExpandableListView listview = new ExpandableListView(
				tv.getContext());
		final AlertDialog dialog = AndroidUtil.genCustomViewDialog(
				tv.getContext(), listview).show();
		listview.setAdapter(new HuBeiQuYuAdapter(tv.getContext(),
				new ShenFenAdapter.OnSelect() {

					@Override
					public void onselect(String gid, String gname,
							String childid, String cname) {
						dialog.dismiss();
						tv.setTag(childid);
						tv.setText(cname);
					}
				}));

	}

	private void initData() {

		BaseControl.get(API.URL + "GetUserForPerfect?userid=%s",
		// BaseControl.get(API.URL + "GetUserForPerfect?userid=%s",
				new AjaxCallBack<String>() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {
							data = t;

							JSONObject jo = new JSONObject(t).optJSONArray(
									"data").getJSONObject(0);

							System.out.println("==");

							initUserInfo(BYQUserInfo.formatBaseInfo2HashMap(jo));
							getdata4BaseInfo();
						} catch (Exception e) {
							e.printStackTrace();
							onFailure(e, 0, e.getMessage());
						}
					}

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						Tip.show("" + t.getMessage());
					}
				}, UserManger.getInstance().userid);

	}

	public void getdata4BaseInfo() {
		BaseControl.get(API.URL + "GetUserInfoForLocal?userid=%s",
				new AjaxCallBack<String>() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {
							final JSONArray optJSONArray = new JSONObject(t)
									.optJSONArray("data");
							initUserMonlog(optJSONArray.getJSONObject(0)
									.optString("monolog"));
							initZEYOUInfo(t);
							setEditMode(false);
						} catch (Exception e) {
							e.printStackTrace();
							onFailure(e, 0, e.getMessage());
						}
					}

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						Tip.show("" + t.getMessage());
					}
				}, UserManger.getInstance().userid);
	}

	protected void setEditMode(boolean b) {
		((TextView) findViewById(R.id.textView1))
				.setCompoundDrawablesRelativeWithIntrinsicBounds(
						R.drawable.ic_mine_data_btn_cancl_p, 0, 0, 0);
		((TextView) findViewById(R.id.textView1)).setTextColor(getResources()
				.getColor(R.color.gray));
		((TextView) findViewById(R.id.textView2))
				.setCompoundDrawablesRelativeWithIntrinsicBounds(
						R.drawable.ic_mine_data_btn_write_n, 0, 0, 0);
		((TextView) findViewById(R.id.textView2)).setTextColor(getResources()
				.getColor(R.color.theme));
		((TextView) findViewById(R.id.textView3))
				.setCompoundDrawablesRelativeWithIntrinsicBounds(
						R.drawable.ic_mine_data_btn_save_p, 0, 0, 0);
		((TextView) findViewById(R.id.textView3)).setTextColor(getResources()
				.getColor(R.color.gray));
		((TextView) findViewById(R.id.textView1)).setEnabled(false);
		((TextView) findViewById(R.id.textView3)).setEnabled(false);
		setAllChildViewEnable(container, (false));
		setAllChildViewEnable(container_neixingaobai, (false));
		setAllChildViewEnable(container_zeyou, (false));

	}

	@SuppressLint("InflateParams")
	private void initUserMonlog(String s) {
		container_neixingaobai.removeAllViews();
		addTitleView(container_neixingaobai, "内心独白");
		View view = getActivity().getLayoutInflater().inflate(R.layout.monlog,
				null);
		tv_monlog = (EditText) view.findViewById(R.id.tv_condubai);
		tv_monlog.setText(s);
		monolog = s;
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		container_neixingaobai.addView(view, lp);
	}

	private void initUserInfo(HashMap<String, KV> key_chinse) {
		final LinearLayout container2 = container;
		container2.removeAllViews();
		String name = "个人信息";
		if (!TextUtils.isEmpty(UserManger.getInstance().username)) {
			name += ":" + UserManger.getInstance().username + "(ID:" + userID
					+ ")";
		}
		addTitleView(container2, name);

		final HashMap<String, KV> key_chinse2 = key_chinse;
		setUserInfo2View(container2, key_chinse2);
	}

	public void setUserInfo2View(final LinearLayout container2,
			final HashMap<String, KV> key_chinse2) {
		Set<String> set = key_chinse2.keySet();
		final Iterator<String> iterator = set.iterator();
		while (iterator.hasNext()) {
			final String next = iterator.next();
			final KV kv = key_chinse2.get(next);
			addInfo(container2, kv.keyChinese, kv.valueChinese, kv.value,
					kv.key, false);
		}
	}

	String data_ZEYOU = "";

	HashMap<String, KV> mapsgetZEYOUInfo;

	private void initZEYOUInfo(String getuserforrequest) throws JSONException {
		container_zeyou.removeAllViews();
		mapsgetZEYOUInfo = BYQUserInfo.getZEYOUInfo(getuserforrequest);
		Set<String> setgetZEYOUInfo = mapsgetZEYOUInfo.keySet();
		final Iterator<String> iteratorgetZEYOUInfo = setgetZEYOUInfo
				.iterator();
		addTitleView(container_zeyou, "择友要求");
		while (iteratorgetZEYOUInfo.hasNext()) {
			final String next = iteratorgetZEYOUInfo.next();
			final KV kv = mapsgetZEYOUInfo.get(next);
			addInfo(container_zeyou, kv.keyChinese, kv.valueChinese, kv.value,
					kv.key, true);
		}

	}

	@SuppressLint("InflateParams")
	private void addTitleView(LinearLayout container2, String string) {
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.title_gray, null);
		TextView tv = (TextView) view.findViewById(R.id.title);
		tv.setText(string);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		container2.addView(view, lp);

	}

	@SuppressLint("InflateParams")
	protected void addInfo(LinearLayout container2, String name,
			String chineseValue, Object opt, String key, boolean isConfit) {
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.item_edit_userinfo, null);
		TextView tv = (TextView) view.findViewById(R.id.textView1);
		tv.setText(name.concat(":"));
		TextView et = (TextView) view.findViewById(R.id.editText1);
		et.setText(chineseValue);
		et.setTag(opt == null ? "" : opt.toString());
		et.setTag(R.id.keyname, key);
		et.setEnabled(isEditAble);
		if (isConfit) {
			setEditDialogWithSE(et, key, name);
		}
		else {

			setEditDialog(et, key, name);
		}
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		container2.addView(view, lp);
	}

	/**
	 * 格式化择友修改所需要的data数据
	 * 
	 * @param regist_email2
	 * @return
	 */
	private String initZEYOUpostDataString(TextView regist_email2) {
		if (mapsgetZEYOUInfo == null) {
			initData();
			return null;
		}
		// 577$583$573$574$576,1,21$26,161$181,1$4,1,1
		// data=572,1,20,83,160,180,1,4,1,1
		// data_ZEYOU = container_zeyou.getChildAt(index)
		StringBuffer data_afteredit = new StringBuffer();
		// data_afteredit.append("0,0");
		data_afteredit.append("0,");
		for (int i = 0; i < KEYLIST4ZEYOUINFO.size(); i++) {
			final String other = KEYLIST4ZEYOUINFO.get(i);
			Lg.d("ZEYOU:name" + other);
			for (int j = 0; j < container_zeyou.getChildCount(); j++) {
				final View findViewById = container_zeyou.getChildAt(j)
						.findViewById(R.id.editText1);
				if (findViewById != null) {
					// Lg.d("ZEYOU:getview" + j+findViewById);
					String name = (String) findViewById.getTag(R.id.keyname);
					if (name.equals(other)) {
						data_afteredit.append(findViewById.getTag());
					}
				}

			}
			if (i < KEYLIST4ZEYOUINFO.size() - 1) {
				data_afteredit.append(",");
			}
		}
		Lg.d("ZEYOU:" + data_afteredit.toString());
		return data_afteredit.toString();// TODO
	}

	/**
	 * 设置一个有起止条件的弹出窗口,弹出两个窗口
	 * 
	 * @param et
	 * @param key
	 * @param keyChinese
	 */
	private void setEditDialogWithSE(final TextView et, final String key,
			final String keyChinese) {
		et.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int type = KEYLIST4ZEYOUINFO.indexOf(key);
				et.setTag(R.id.keyname, KEYLIST4ZEYOUINFO.get(type));
				switch (type) {
				case 2:
					genIntInputDialog2(new String[] { "最低身高(厘米/cm)",
							"最高身高(厘米/cm)" }, "请输入一个整数", et,
							new ZEYOUCallBack() {

								@Override
								public void callback(TextView regist_email2) {
									updateZeyouInfo(initZEYOUpostDataString(regist_email2));
								}
							});
					break;
				case 1:
					genIntInputDialog2(new String[] { "最小年龄(周岁)", "最大年龄(周岁)" },
							"请输入一个整数", et, new ZEYOUCallBack() {

								@Override
								public void callback(TextView regist_email2) {
									updateZeyouInfo(initZEYOUpostDataString(regist_email2));

								}
							});
					break;
				case 0:
					genRaidoListDialog(
							BYQUserInfo.getReadableArray(keyChinese), et);
					break;
				case 5:
					genRaidoListDialog(BYQUserInfo.getReadableArray("形象照"), et);
					break;
				case 3:
					// TODO
					genRaidoListDialog2(
							BYQUserInfo.getReadableArray(keyChinese), et,
							new ZEYOUCallBack() {
								@Override
								public void callback(TextView regist_email2) {
									updateZeyouInfo(initZEYOUpostDataString(regist_email2));
								}
							});
					break;
				case 4:
					genRaidoListDialog(
							BYQUserInfo.getReadableArray(keyChinese), et);
					break;
				}
				// updateZeyouInfo(data_ZEYOU);
			}
		});

	}

	private void setEditDialog(final TextView et, final String key,
			final String keyChinese) {
		et.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int type = KEYLIST4USERINFO.indexOf(key);
				switch (type) {
				case 0:
					genDateDialog(et);
					break;
				case 1:
					genIntInputDialog("身高(厘米/cm)", "请输入一个整数", et);
					break;
				case 2:
					genIntInputDialog("体重(千克/kg)", "请输入一个整数", et);
					break;
				case 7:// 籍贯
					showShenFenSelectDialog(et);
					break;
				case 8:// 区域
					showHuBeiQuYuSelectDialog(et);
					break;
				default:
					genRaidoListDialog(
							BYQUserInfo.getReadableArray(keyChinese), et);
					break;
				}

			}
		});

	}

	// 03-26 23:56:55.314: I/String(5002):
	// http://121.41.26.194:8080/by_webservice/RegUserForPerfect?userid=162345&data=2015-3-26,1,5,0,0,2,0,1,1,3,6,4

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		if (findViewById(R.id.textView3) == v) {
			// http://121.41.26.194:8080/by_webservice/RegUserForPerfect?userid=162000&data=2014-09-11,181,49.5,1,1,1,1,1,1,1,1,1,1,1,白羊座,1,1$2$3
			StringBuffer data_afteredit = new StringBuffer();

			for (int i = 0; i < KEYLIST4USERINFO.size(); i++) {
				final String other = KEYLIST4USERINFO.get(i);
				for (int j = 0; j < container.getChildCount(); j++) {
					final View findViewById = container.getChildAt(j)
							.findViewById(R.id.editText1);
					if (findViewById != null) {
						// Lg.d("ZEYOU:getview" + j+findViewById);
						String name = (String) findViewById
								.getTag(R.id.keyname);
						if (name.equals(other)) {
							String s = "0";
							if (findViewById.getTag() != null) {
								s = String.valueOf(findViewById.getTag());
								System.out.println("interest ," + s);
								// 兴趣是多选的 所以上传的时候用$号隔开
								if (name.equals("interest")) {
									if (TextUtils.isEmpty(s)) {
										s = "0";
									}
									s = s.replace(",", "$");
									System.out.println("interest $" + s);
								}
							}
							data_afteredit.append(s);
						}
					}

				}
				if (i < KEYLIST4USERINFO.size() - 1) {
					data_afteredit.append(",");
				}
			}
			System.out.println("post baseinfo:" + data_afteredit);
			// JSONObject jpo = new JSONObject();
			// for (int i = 0; i < childViews; i++) {
			// //TODO ORDER
			// final View findViewById = container.getChildAt(i).findViewById(
			// R.id.editText1);
			// if (findViewById != null && findViewById instanceof TextView) {
			// data_afteredit.append(findViewById.getTag());
			// if (i < childViews - 1) {
			// data_afteredit.append(",");
			// }
			// try {
			// jpo.put(findViewById.getTag(R.id.keyname).toString(),
			// findViewById.getTag());
			// } catch (JSONException e) {
			// e.printStackTrace();
			// }
			// }
			// }
			// System.out.println("post baseinfo:" + jpo.toString());
			BaseControl.get(API.URL + "RegUserForPerfect?userid=%s&data=%s",
					new AjaxCallBack<String>() {
						@Override
						public void onSuccess(String t) {
							super.onSuccess(t);
							try {

								String jo = new JSONObject(t).optString("msg");
								if ("2".equals(jo)) {
									Tip.show("修改成功");
								}
								else {
									Tip.show("修改失败");
								}
							} catch (Exception e) {
								e.printStackTrace();
								onFailure(e, 0, e.getMessage());
							}
						}

						@Override
						public void onFailure(Throwable t, int errorNo,
								String strMsg) {
							super.onFailure(t, errorNo, strMsg);
							Tip.show("" + t.getMessage());
						}
					}, UserManger.getInstance().userid, data_afteredit);
			if (!TextUtils.isEmpty(tv_monlog.getText())
					&& !tv_monlog.getText().toString().equals(monolog)) {
				BaseControl.get(
						API.URL + "RegUserForMonolog?userid=%s&data=%s",
						new AjaxCallBack<String>() {
							@Override
							public void onSuccess(String t) {
								super.onSuccess(t);
								try {

									String jo = new JSONObject(t)
											.optString("msg");
									if ("1".equals(jo)) {
										Tip.show("修改内心独白成功");
									}
									else {
										Tip.show("修改内心独白失败");
									}
								} catch (Exception e) {
									e.printStackTrace();
									onFailure(e, 0, e.getMessage());
								}
							}

							@Override
							public void onFailure(Throwable t, int errorNo,
									String strMsg) {
								super.onFailure(t, errorNo, strMsg);
								Tip.show("" + t.getMessage());
							}
						}, UserManger.getInstance().userid, URLEncoder
								.encode(tv_monlog.getText().toString()));
			}
			updateZeyouInfo(initZEYOUpostDataString(null));
			((TextView) findViewById(R.id.textView1))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_cancl_p, 0, 0, 0);
			((TextView) findViewById(R.id.textView1))
					.setTextColor(getResources().getColor(R.color.gray));
			((TextView) findViewById(R.id.textView2))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_write_n, 0, 0, 0);
			((TextView) findViewById(R.id.textView2))
					.setTextColor(getResources().getColor(R.color.theme));
			((TextView) findViewById(R.id.textView3))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_save_p, 0, 0, 0);
			((TextView) findViewById(R.id.textView3))
					.setTextColor(getResources().getColor(R.color.gray));
			((TextView) findViewById(R.id.textView1)).setEnabled(false);
			((TextView) findViewById(R.id.textView3)).setEnabled(false);
			setAllChildViewEnable(container, (false));
			setAllChildViewEnable(container_neixingaobai, (false));
			setAllChildViewEnable(container_zeyou, (false));
		}
		if (findViewById(R.id.textView2) == v) {
			((TextView) findViewById(R.id.textView1))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_cancl_n, 0, 0, 0);
			((TextView) findViewById(R.id.textView1))
					.setTextColor(getResources().getColor(R.color.theme));
			((TextView) findViewById(R.id.textView2))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_write_p, 0, 0, 0);
			((TextView) findViewById(R.id.textView2))
					.setTextColor(getResources().getColor(R.color.gray));
			((TextView) findViewById(R.id.textView3))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_save_n, 0, 0, 0);
			((TextView) findViewById(R.id.textView3))
					.setTextColor(getResources().getColor(R.color.theme));
			((TextView) findViewById(R.id.textView1)).setEnabled(true);
			((TextView) findViewById(R.id.textView3)).setEnabled(true);
			setAllChildViewEnable(container, (true));
			setAllChildViewEnable(container_neixingaobai, (true));
			setAllChildViewEnable(container_zeyou, (true));
		}
		if (findViewById(R.id.textView1) == v) {
			((TextView) findViewById(R.id.textView1))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_cancl_p, 0, 0, 0);
			((TextView) findViewById(R.id.textView1))
					.setTextColor(getResources().getColor(R.color.gray));
			((TextView) findViewById(R.id.textView2))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_write_n, 0, 0, 0);
			((TextView) findViewById(R.id.textView2))
					.setTextColor(getResources().getColor(R.color.theme));
			((TextView) findViewById(R.id.textView3))
					.setCompoundDrawablesRelativeWithIntrinsicBounds(
							R.drawable.ic_mine_data_btn_save_p, 0, 0, 0);
			((TextView) findViewById(R.id.textView3))
					.setTextColor(getResources().getColor(R.color.gray));
			((TextView) findViewById(R.id.textView1)).setEnabled(false);
			((TextView) findViewById(R.id.textView3)).setEnabled(false);
			setAllChildViewEnable(container, (false));
			setAllChildViewEnable(container_neixingaobai, (false));
			setAllChildViewEnable(container_zeyou, (false));
			initData();
		}
	}

	/**
	 * userid=162000&data=地区要求(以setarea字段的city为主,多个地区使用$号隔开)性别要求，年龄要求（22$26），
	 * 身高要求(161$171)，学历要求(1(初中以上)$4(博士以下))，婚史状态，是否有照片<br>
	 * http://121.41.26.194:8080/by_webservice/RegUserForRequest?userid=162399&
	 * data=0,1,20,83,160,180,1,4,1,1 1$4,1,1
	 * 
	 * @param data
	 */
	void updateZeyouInfo(String data) {
		if (data == null) {
			return;
		}
		BaseControl.get(API.URL + "RegUserForRequest?userid=%s&data=%s",
				new AjaxCallBack<String>() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {

							String jo = new JSONObject(t).optString("msg");
							if ("2".equals(jo)) {
								Tip.show("更新择友条件成功");
							}
							else {
								Tip.show("更新择友条件失败");
							}
						} catch (Exception e) {
							e.printStackTrace();
							onFailure(e, 0, e.getMessage());
						}
					}

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						Tip.show("" + t.getMessage());
					}
				}, UserManger.getInstance().userid, data);
	}

}
